<?php
/* 
* @Title:  [商品分类模型]
* @Author: {chenlei} [chenleib5@126.com]
* @Date:   2014-04-05 16:38:31
* @Last Modified by:   Administrator
* @Last Modified time: 2014-04-12 22:16:28
* @Copyright:  [hn7m.com]
*/
class CategoryModel extends Model{
	public $table='category';

	/**
	 * [$validate 自动验证]
	 * @var array
	 */
	public $validate=array(
		array('cname','nonull','分类名称不能为空',2,3),
		array('tid','regexp:/^[1-9]\d*?$/','必须选择商品类型',2,3)
		);

	/**
	 * [add_data 添加分类]
	 */
	public function add_data(){
		$upload = new Upload();
		$upfiles = $upload->upload();
		// p($upfiles);die();
		$cshow=(Q('post.cshow')=='on')?1:0;
		$cpid=Q('get.cid',0,'intval');
		$clogo=(!empty($upfiles))?$upfiles[0]['path']:'';
		$data=array(
			'cname'	=>Q('cname', NULL, 'htmlspecialchars'),
			'cpid'	=>$cpid,
			'csort'	=>Q('csort', 100, 'intval'),
			'clogo'	=>$clogo,
			'cshow'	=>$cshow,
			'qm_type_tid'=>Q('tid', 0, 'intval')
			);
		return $this->add($data);
	}

	/**
	 * [get_data 获得所有的数据]
	 * @return [type] [description]
	 */
	public function get_data($field=NULL,$where=NULL,$csort=NULL){
		return $this->field($field)->where($where)->order($csort)->findAll();
	}

	/**
	 * [get_one_data 获得一条数据]
	 * @return [type] [description]
	 */
	public function get_one_data($cid,$field=NULL){
		return $this->field($field)->where(array('cid'=>$cid))->find();
	}
	/**
	 * [get_onedate_value 获得一个字段值]
	 * @param  [type] $cid  [分类cid]
	 * @param  [type] $need [需要的字段值]
	 * @return [type]       [description]
	 */
	public function get_onedate_value($cid,$need){
		return $this->where(array('cid'=>$cid))->getField($need);
	}
	/**
	 * [get_father_cid 获得自己所有父级name]
	 * @param  [type] $pid [description]
	 * @return [type]      [description]
	 */
	public function get_father_cid($pid){
		$cidArr = $this->field('cid,cpid,cname')->findAll();
		$tempArr = array();
		foreach ($cidArr as $v) {
			if($v['cid'] == $pid){
				$tempArr[] = $v['cname'];
				$tempArr = array_merge($tempArr, $this->get_father_cid($v['cpid']));
			}
		}
		return $tempArr;
	}

	/**
	 * [get_chose_cate 获得当前分类可以选择作为自己的父级]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_chose_cate($cid){
		$cidArr = $this->get_son_id($cid);
		$cidArr[] = $cid;
		$cidArr = implode(',', $cidArr);

		$sql = "SELECT `cid`,`cname`,`cpid` FROM qm_category WHERE cid NOT IN ($cidArr)";
	
		$data = $this->query($sql);
		return $data;
	}

	/**
	 * [get_son_id 获得自己所有子级id]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function get_son_id($cid){
		$cidArr = $this->field('cid,cpid')->all();
		$tempArr = array();
		foreach ($cidArr as $v) {
			if($v['cpid'] == $cid){
				$tempArr[] = $v['cid'];
				$tempArr = array_merge($tempArr, $this->get_son_id($v['cid']));
			}
		}
		return $tempArr;
	}

	/**
	 * [edit_data 修改分类]
	 * @return [type] [description]
	 */
	public function edit_data($cid){
		$cshow=(Q('post.cshow')=='on')?1:0;
		$this->data['cshow']=$cshow;
		$upload = new Upload();
		$upfiles = $upload->upload();
p($_FILES);die;
		//更新logo图片
		if(!empty($_FILES['clogo']['name'])){
			//$upload->error是文件上传类的属性,详见手册106页
			if($upload->error){
				$this->error=$upload->error;
				return false;
			}
			//如果上传了新的缩略图,则删除原缩略图
			$path=$this->field('clogo')->find($cid);
			$path=$path['clogo'];
			$thumb=trim($path,strrchr($path,'.')).C('THUMB_ENDFIX').strrchr($path,'.');
			$path=ROOT_PATH.$path;
			$thumb=ROOT_PATH.$thumb;

			!is_file($thumb)||unlink($thumb);
			!is_file($path)||unlink($path);
			p($_FILES);die;
			$this->data['clogo']=$upfiles[0]['path'];
		}
		//修改类型
		$this->data['qm_type_tid']=Q('tid',0,'intval');
		return $this->where(array('cid'=>$cid))->update();
	}
	/**
	 * [have_data 判断当前分类下面是否有商品或者是否有子分类]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function have_data($cid){
		$cid = $this->where(array('cpid'=>$cid))->getField('cid');
		if($cid){
			$this->error = '请先删除子分类';
			return false;
		}

		// $aid = K('Article')->where(array('category_cid'=>$cid))->getField('aid');

		// if($aid){
		// 	$this->error = '请先删除该分类下面的文章';
		// 	return false;
		// }

		return true;
	}

	/**
	 * [del_data 根据主键id删除数据]
	 * @param  [type] $cid [description]
	 * @return [type]      [description]
	 */
	public function del_data($cid){
		$this->delete($cid);
	}
	/**
	 * [get_max 获得当前分类下的某个字段最大记录值]
	 * @param  [type] $field [description]
	 * @return [type]        [description]
	 */
	public function get_max($field,$cid){
		return $this->where(array('cpid'=>$cid))->max($field);
	}
}
?>